package com.liang.blog.system.controller;

import com.liang.blog.common.result.Result;
import com.liang.blog.model.BlogMenu;

import com.liang.blog.security.annotation.HasPermission;
import com.liang.blog.security.utils.BlogSecurityUtil;
import com.liang.blog.system.service.BlogMenuService;
import com.liang.blog.vo.menu.BlogMenuVo;
import com.liang.blog.vo.menu.MenuPermissionVo;
import com.liang.blog.vo.menu.MenuSelectVo;
import com.liang.blog.vo.menu.RouterVo;
import com.liang.blog.vo.user.LoginUserVo;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2023-08-17-16:51
 */
//@Api(tags = "系统菜单")
@RestController
@RequestMapping("/menu")
public class BlogMenuController {

    @Autowired
    private BlogMenuService blogMenuService;

//    @ApiOperation("获取系统菜单, 生成前端路由")
    @GetMapping("routerInfo")
    public Result routerInfo(){
        Long userId = BlogSecurityUtil.getUserId();
        List<BlogMenu> menuList = null;
        if (userId.intValue() == 1){
             menuList = blogMenuService.getMenuAll();
        } else {
             menuList = blogMenuService.getBlogMenuByUid(userId);
        }
        return Result.ok(blogMenuService.buildRouter(menuList));
    }

//    @ApiOperation("通过角色菜单中间表，获取菜单信息")
    @GetMapping("getMenuByRoleIdAndMenuId/{roleId}")
    public List<MenuPermissionVo> getMenuByRoleIdAndMenuId(@PathVariable(value = "roleId") int roleId){
        return blogMenuService.getMenuByRoleIdAndMenuId(roleId);
    }

    /**
     * 获取全部的菜单信息
     * @param parentId
     * @return
     */
    @GetMapping("getMenuAllByParentId/{parentId}")
    public Result getMenuAllByParentId(@PathVariable Long parentId){
        List<BlogMenuVo> blogMenuVos = blogMenuService.getBlogMenuAllByParentId(parentId);
        return Result.ok(blogMenuVos);
    }

    @PostMapping("getMenuList")
    @HasPermission("system:menu:read")
    public Result getMenuList(@RequestBody BlogMenu blogMenu){
        List<BlogMenu> menuList = blogMenuService.buildMenu(blogMenu);
        return Result.ok(menuList);
    }

    /**
     * 保存菜单
     * @param blogMenu
     * @return
     */
    @PostMapping("saveMenu")
    @HasPermission("system:menu:add")
    public Result saveMenu(@RequestBody BlogMenu blogMenu){
        blogMenuService.insertMenu(blogMenu);
        return Result.ok();
    }

    /**
     * 获取级联菜单信息
     * @return
     */
    @GetMapping("getMenuSelectVoAll")
    public Result getMenuSelectVoAll(){
        List<MenuSelectVo> blogMenuAll = blogMenuService.getBlogMenuAll();
        return Result.ok(blogMenuAll);
    }

    /**
     * 修改菜单
     * @param blogMenu
     * @return
     */
    @PutMapping("updateMenu")
    @HasPermission("system:menu:update")
    public Result updateMenu(@RequestBody BlogMenu blogMenu){
        blogMenuService.updateMenu(blogMenu);
        return Result.ok();
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("deleteMenu/{id}")
    @HasPermission("system:menu:delete")
    public Result deleteMenu(@PathVariable Long id){
        blogMenuService.deleteMenu(id);
        return Result.ok();
    }
}
